package com.xhwl.logistics.service.business;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.logistics.bo.VehicleBlacklistBO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleBlacklistHttpBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleBlacklistBO;
import com.xhwl.logistics.component.LookupVehicleParkingLotHandler;
import com.xhwl.logistics.converter.ToPage;
import com.xhwl.logistics.converter.mapstruct.VehicleBlacklistConverter;
import com.xhwl.logistics.entity.VehicleBlacklist;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.service.impl.VehicleBlacklistServiceImpl;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleIntegratedConfigRedisServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotBlacklistHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//@Service
public class VehicleBlacklistBusinessService {

    @Autowired
    private VehicleBlacklistServiceImpl vehicleBlacklistService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private QueryProjectService queryProjectService;

    @Autowired
    private LookupVehicleParkingLotHandler lookupVehicleParkingLotHandler;

    @Autowired
    private VehicleIntegratedConfigRedisServiceImpl vehicleIntegratedConfigRedisService;

    @Autowired
    private VehicleBlacklistConverter vehicleBlacklistConverter;

    @Autowired
    private ToPage toPage;

    public IPage<VehicleBlacklistBO> queryBlacklist(QueryVehicleBlacklistBO queryVehicleBlacklistBO, PageBO pageBO) {
        Page<VehicleBlacklist> vehicleBlacklistPage = vehicleBlacklistService.page(pageBO.toPage(VehicleBlacklist.class), vehicleBlacklistService.conditionForQueryBlacklists(queryVehicleBlacklistBO));

        return toPage.toPage(vehicleBlacklistPage, vehicleBlacklistConverter.vehicleBlacklistList2VehicleBlacklistBOList(vehicleBlacklistPage.getRecords()));
    }

    public VehicleBlacklistBO detailBlacklist(Integer id) {
        VehicleBlacklist vehicleBlacklist = vehicleBlacklistService.getById(id);

        return vehicleBlacklistConverter.vehicleBlacklist2VehicleBlacklistBO(vehicleBlacklist);
    }

    @Transactional(rollbackFor = {Exception.class})
    public void addBlacklist(CreateVehicleBlacklistBO createVehicleBlacklistBO) {
        Integer projectId = createVehicleBlacklistBO.getProjectId();

        ProjectBO projectBO = queryProjectService.queryProject(projectId);
        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = vehicleParkingLotService.queryParkingLot(projectId, createVehicleBlacklistBO.getParkingLotName());
        createVehicleBlacklistBO.setParkingLotName(vehicleParkingLotSecondBO.getParkingLotName());

        VehicleBlacklist vehicleBlacklist = vehicleBlacklistConverter.createVehicleBlacklistBO2VehicleBlacklist(createVehicleBlacklistBO, projectBO.getProjectName(), vehicleParkingLotSecondBO.getId());
        vehicleBlacklistService.save(vehicleBlacklist);

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = vehicleIntegratedConfigRedisService.queryIntegratedConfigByProjectId(projectId);
        IVehicleParkingLotBlacklistHandler vehicleParkingLotBlacklistHandler = lookupVehicleParkingLotHandler.lookForBlacklistHandler(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
        vehicleParkingLotBlacklistHandler.addCarBlack(vehicleIntegratedConfigBO, vehicleBlacklistConverter.createVehicleBlacklistBO2CreateVehicleBlacklistHttpBO(createVehicleBlacklistBO));
    }

    public void addBlacklists(List<String> plateNumberList, Integer projectId) {
        ProjectBO projectBO = queryProjectService.queryProject(projectId);

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = vehicleParkingLotService.queryParkingLot(projectId, null);

        List<VehicleBlacklist> vehicleBlacklistList = vehicleBlacklistConverter.toVehicleBlacklistList(plateNumberList,
                projectId, projectBO.getProjectName(),
                vehicleParkingLotSecondBO.getId(), vehicleParkingLotSecondBO.getParkingLotName(),
                "xxl-job");
        vehicleBlacklistService.saveBatch(vehicleBlacklistList);
    }

    @Transactional(rollbackFor = {Exception.class})
    public void deleteBlacklist(DeleteVehicleBlacklistBO deleteVehicleBlacklistBO) {
        VehicleBlacklist vehicleBlacklist = vehicleBlacklistService.getById(deleteVehicleBlacklistBO.getId());

        vehicleBlacklistService.deleteBlacklist(deleteVehicleBlacklistBO);

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = vehicleIntegratedConfigRedisService.queryIntegratedConfigByProjectId(vehicleBlacklist.getProjectId());
        IVehicleParkingLotBlacklistHandler vehicleParkingLotBlacklistHandler = lookupVehicleParkingLotHandler.lookForBlacklistHandler(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
        vehicleParkingLotBlacklistHandler.deleteCarBlack(vehicleIntegratedConfigBO, new DeleteVehicleBlacklistHttpBO(vehicleBlacklist.getPlateNumber()));
    }

    public void updateBlacklist(UpdateVehicleBlacklistBO updateVehicleBlacklistBO) {
        VehicleBlacklist vehicleBlacklist = vehicleBlacklistConverter.updateVehicleBlacklistBO2VehicleBlacklist(updateVehicleBlacklistBO);
        vehicleBlacklistService.updateById(vehicleBlacklist);
    }

}
